<template>
  <div class="vertical-container">
    <a-card 
      v-for="item in cardData" 
      :key="item.id"
      class="salary-card"
      @click="goToDetail(item.id)"
    >
      <template #header>
        <div class="card-header">
          <span class="text-ellipsis">{{ item.title }}</span>
        </div>
      </template>
      <p class="text item">总薪酬：{{ formatCurrency(item.total_comp) }}/{{ item.currency_code }}/年</p>
      <p class="text item">月薪：{{ formatCurrency(item.base_monthly_comp) }}/{{ item.currency_code }}/月</p>
      <p class="text item">股票：{{ formatCurrency(item.stock_comp) }}/{{ item.currency_code }}/年</p>
      <template #footer>
        <div class="footer-text">{{ item.location }}</div>
      </template>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { SalariesdataApi } from '@/utils/api.js'


const cardData = ref([])
const router = useRouter()

// 格式化货币显示
const formatCurrency = (amount) => {
  if (!amount) return '0'
  return (amount / 1000).toFixed(0) + 'k'
}

// 跳转到详情页
const goToDetail = (id) => {
  if (id) {
    router.push(`/workplaceroast/${id}`);
  }
}

// 获取最新薪酬数据
const fetchSalaryData = async () => {
  try {
    const response = await SalariesdataApi.getDataList({
      page: 1,
      size: 4
    })
    
    // 提取前4条数据
    cardData.value = response.data.slice(0, 4).map(item => ({
      id: item.id,
      title: item.title,
      total_comp: item.total_comp,
      base_monthly_comp: item.base_monthly_comp,
      stock_comp: item.stock_comp,
      location: `${item.location} | ${item.country}`,
      currency_code: item.currency_code
    }))
  } catch (error) {
    console.error('获取薪酬数据失败:', error)
  }
}

onMounted(() => {
  fetchSalaryData()
})
</script>

<style scoped>
.vertical-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
}

.salary-card {
  width: 100%;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  margin-bottom: 16px;
  cursor: pointer;
}

.salary-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card-header {
  font-weight: bold;
  color: #000;
  font-size: 14px;
}

.text.item {
  margin: 8px 0;
  color: #606266;
  font-size: 14px;
}

.text-ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.footer-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 12px;
  color: #999;
}
</style>